<?php
/**
 * IG_Filter_Get
 *
 * @author maomao.thx@gmail.com
 * @copyright Copyright (c) 2009 Green Shore (http://www.iwgame.com)
 * @version $Id: Get.php 524 2011-05-24 01:44:20Z mmxie $
 */
class IG_Filter_Get
{
    /**
     * default method, strips tags
     *
     * @param string $key
     */
    public static function get($key)
    {
        $filter = new Zend_Filter_StripTags();
        $get = self::toObject();
        return trim($filter->filter($get->$key));

    }

    /**
     * set a post value
     *
     * @param string $key
     * @param string $value
     */
    public static function set($key, $value)
    {
        $_GET[$key] = $value;
    }

    /**
     * returns the whole post array as an object
     *
     * @todo make this method handle array data
     * @return stdClass object
     */
    public static function toObject()
    {
        $get = new stdClass();
        foreach ($_GET as $k => $v) {
            if (is_array($v)) {
                $get->$k = $v;
            } else {
                $get->$k = stripslashes($v);
            }
        }
        return $get;
    }

    /**
     * test whether the key is set
     *
     * @param string $key
     * @return bool
     */
    public static function has($key) {
        if (isset($_GET[$key])) {
            return true;
        }
    }

    /**
     * returns the value without any filters
     *
     * @param string $key
     * @return mixed
     */
    public static function raw($key)
    {
        return $_GET[$key];
    }

    /**
     * filters the value as alpha
     *
     * @param string $key
     * @return string
     */
    public static function alpha($key)
    {
        $filter = new Zend_Filter_Alpha();
        $get =self::toObject();
        return trim($filter->filter($get->$key));
    }

    /**
     * filters the value as an integer
     *
     * @param string $key
     * @return int
     */
    public static function int($key)
    {
        $get = self::toObject();
        return intval($get->$key);
    }

    /**
     * strips and adds slashes. i strip them first because the html editor adds them as well
     *
     * @param string $key
     * @return string
     */
    public static function text($key)
    {
        //you must strip slashes first, as the HTML editors add them
        //by doing this you are able to process both raw HTML and WYSIWYG HTML
        $get = self::toObject();
        return trim(addslashes($get->$key));
    }

    /**
     * returns the value as a floating point #
     *
     * @param string $key
     * @return float
     */
    public static function float($key)
    {
        $get = self::toObject();
        if (is_float($get[$key])) {
            return floatval($get->$key);
        }
    }

    /**
     * returns the value as a timestamp
     * the value is evaluated with zend_date
     *
     * @param string $key
     * @return timestamp
     */
    public static function date($key)
    {
        $get =self::toObject();
        if ($get[$key]) {
            $date = new Zend_Date($get->$key);
            return $date->get(Zend_Date::TIMESTAMP);
        }
    }

    /**
     * dumps the post variables
     *
     */
    public static function dump()
    {
        Zend_Debug::dump($_GET);
    }

}